package com.greenhouse.business.common;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.greenhouse.business.common.websocket.DeviceOnLineStatistic;
import com.greenhouse.business.common.websocket.DeviceWebsocket;
import com.greenhouse.business.common.websocket.GroupWebsocket;
import com.greenhouse.business.common.wraning.WarningComputeService;
import com.greenhouse.business.domain.BizDevice;
import com.greenhouse.business.domain.BizDeviceData;
import com.greenhouse.business.domain.BizGroup;
import com.greenhouse.business.domain.BizGroupAlarm;
import com.greenhouse.business.service.IBizDeviceDataService;
import com.greenhouse.business.service.IBizDeviceService;
import com.greenhouse.business.service.IBizGroupAlarmService;
import com.greenhouse.business.service.IBizGroupService;
import com.greenhouse.protocol.internalmessage.MessageActor;
import com.greenhouse.protocol.internalmessage.MessageCenter;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * @版权 : Copyright (c) 2020 儒安物联技术研发中心
 * @author: 雷扬
 * @E-mail: leiyang@runaiot.com
 * @版本: 1.0
 * @创建日期: 2021年5月8日 下午1:21:15
 * @ClassName DefaultMessageActor
 * @类描述-Description: TODO(这里用一句话描述这个方法的作用)
 * @修改记录:
 * @版本: 1.0
 */
@Component
@Slf4j
public class DefaultMessageActor implements MessageActor {

	@Getter
	int num = 0;

	private final static String id = "business";

	@Autowired
	private IBizDeviceDataService bizDeviceDataService;

	@Autowired
	private WarningComputeService warningComputeService;

	@Autowired
	private IBizGroupService bizGroupService;

	@Autowired
	private IBizDeviceService bizDeviceService;

	@Autowired
	private IBizGroupAlarmService bizGroupAlarmService;

	@Autowired
	private GroupWebsocket groupWebsocket;

	@Autowired
	private DeviceWebsocket deviceWebsocket;

	@Autowired
	private DeviceOnLineStatistic deviceOnLineStatistic;

	@PostConstruct
	public void register() {
		MessageCenter.sub(this);
	}

	@Override
	public void onMessage(Long device, JSONObject message) {
		// TODO Auto-generated method stub
		BizGroupAlarm bizGroupAlarm = new BizGroupAlarm();

		bizGroupAlarm.setDeviceId(device);

		List<BizGroupAlarm> alarms = bizGroupAlarmService.selectBizGroupAlarmList(bizGroupAlarm);

		// 一个device 可能存在多个报警，但是必定在一个大棚中
		BizGroup bizGroup = null;

		if (alarms != null && alarms.size() > 0) {
			bizGroup = bizGroupService.selectBizGroupById(alarms.get(0).getGroupId());
		}

		BizDevice bizDevice = bizDeviceService.selectBizDeviceById(device);

		if (message != null && !message.isEmpty()) {
			// 报警计算
			warningComputeService.onMessage(Long.valueOf(device), message, alarms,
					bizDevice == null ? new BizDevice() : bizDevice, bizGroup == null ? new BizGroup() : bizGroup);

			// 保存数据
			message.entrySet().forEach(entry -> {
				BizDeviceData data = new BizDeviceData();
				data.setDeviceId(Long.valueOf(device));
				data.setAttributeCode(entry.getKey());
				data.setValue(Double.valueOf(entry.getValue().toString()));
				bizDeviceDataService.insertBizDeviceData(data);
			});

		}

		message.put("deviceId", device);

		// 推送消息
		if (bizGroup != null) {
			groupWebsocket.sendInfo(bizGroup.getGroupId().toString(), JSONObject.toJSONString(message));
		}

		if (bizDevice != null) {
			deviceWebsocket.sendInfo(device.toString(), JSONObject.toJSONString(message));
		}

		log.error(message.toJSONString());
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return id;
	}

	@Override
	public void onLine(int i) {
		num = i;
		// TODO Auto-generated method stub
		int device = bizDeviceService.selectBizDeviceList(new BizDevice()).size();
		if (device == 0) {
			JSONObject message = new JSONObject();
			message.put("online", 0);
			message.put("offline", 0);
			deviceOnLineStatistic.sendInfo(message.toJSONString());
		} else {
			JSONObject message = new JSONObject();
			message.put("online", i);
			message.put("offline", device - i);
			deviceOnLineStatistic.sendInfo(message.toJSONString());
		}
	}

	@Override
	public void offLine(int i) {
		num = i;
		// TODO Auto-generated method stub
		onLine(i);
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		int device = bizDeviceService.selectBizDeviceList(new BizDevice()).size();
		if (device == 0) {
			JSONObject message = new JSONObject();
			message.put("online", 0);
			message.put("offline", 0);
			deviceOnLineStatistic.sendInfo(message.toJSONString());
		} else {
			JSONObject message = new JSONObject();
			message.put("online", num);
			message.put("offline", device - num);
			deviceOnLineStatistic.sendInfo(message.toJSONString());
		}
	}

}
